﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using School.Entities;
using System.Collections;
using System.IO;

namespace School.Admission
{
    public class Custody
    {
        private static SchoolEntities db = new SchoolEntities();

        public static Boolean Add( SD_Custody   custody)
        {
            try
            {
                db.AddToSD_Custody(custody);
                db.SaveChanges();

                return true;
            }

            catch
            {
                return false;

            }
        }

        public static SD_Custody Single(long id)
        {
            return db.SD_Custody.Single(x => x.Id == id);
        }




        public static SD_Custody SingleByStudentId(long sid)
        {
            try
            {
                return db.SD_Custody.Single(x => x.Student_Id == sid);


            }
            catch
            {
                return null;
            }


        }



        public static IEnumerable<SD_Custody> All(Hashtable items)
        {
            SchoolEntities db = new SchoolEntities();

            var query = from custody in db.SD_Custody select custody;

            foreach (DictionaryEntry item in items)
            {
                if (item.Key == "Ar_FirstName")
                {
                    query = query.Where(c => c.Ar_FirstName == item.Value);
                }

                if (item.Key == "Ar_SecondName")
                {
                    query = query.Where(c => c.Ar_SecondName == item.Value);
                }

                if (item.Key == "Ar_ThirdName")
                {
                    query = query.Where(c => c.Ar_ThirdName == item.Value);
                }


                if (item.Key == "Ar_FourthName")
                {
                    query = query.Where(c => c.Ar_FourthName == item.Value);
                }


            }
            return query.ToList();
        }

        public static void Update(SD_Custody custody)
        {
            SD_Custody c = db.SD_Custody.Single(x => x.Id == custody.Id);


            c.Id = custody.Id;

            c.Student_Id = custody.Student_Id;
            c.RelationToStudent = custody.RelationToStudent;
            c.ParentsSocialStatus_Id = custody.ParentsSocialStatus_Id;
            c.CustodyType_Id = custody.CustodyType_Id;
            c.StudentLivingPlace_Id = custody.StudentLivingPlace_Id;

            c.Ar_FirstName = custody.Ar_FirstName;
            c.Ar_SecondName = custody.Ar_SecondName;
            c.Ar_ThirdName = custody.Ar_ThirdName;
            c.Ar_FourthName = custody.Ar_FourthName;

            c.En_FirstName = custody.En_FirstName;
            c.En_SecondName = custody.En_SecondName;
            c.En_ThirdName = custody.En_ThirdName;
            c.En_FourthName = custody.En_FourthName;

            c.Gregorian_BirthDate = custody.Gregorian_BirthDate;
            c.Hijri_BirthDate = custody.Hijri_BirthDate;
            c.Identification_Number = custody.Identification_Number;
            c.Pasport_Number = custody.Pasport_Number;
            c.Qualification_Id = custody.Qualification_Id;
            c.Specialization = custody.Specialization;
            c.PointOfGraduation = custody.PointOfGraduation;
            c.Greggorian_GraduationDate = custody.Greggorian_GraduationDate;
            c.Hijri_GraduationDate = custody.Hijri_GraduationDate;
            c.HomeAddress_Id = c.HomeAddress_Id;
            c.WorkAddress_Id = custody.WorkAddress_Id;
            c.Nationality_Id = custody.Nationality_Id;

            db.SaveChanges();

        }

        public static Boolean Delete(long id)
        {
            try
            {
                db.SD_Custody.DeleteObject(db.SD_Custody.Single(x => x.Id == id));
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

    }
}
